<?php
/**
 * Tests for TS_Auth_Adapter
 *
 * PHP version 5
 *
 * @package    TV
 * @subpackage Tests
 * @author     Mike Bevz <mike.bevz@tourstart.dk>
 * @copyright  2007-2008 (c) Mike Bevz
 * @version    SVN: $Id: AnalyzerTests.php 9 2008-12-01 01:17:49Z michael.bevz $
 *
 *
 */

/**
 * Includes below
 */
require_once dirname(__FILE__).'/../testsConfig.php';
require_once 'PHPUnit/Framework.php';
require_once 'Zend/Auth/Result.php';

/**
 * Model for message sent to client
 *
 * PHP version 5
 *
 * @package    Tourstart
 * @subpackage Tests
 * @author     Mike Bevz <mike.bevz@tourstart.dk>
 * @copyright  2007-2008 (c) Tourstart.dk
 * @license    http://tourstart.dk/license Tourstart License
 * @version    SVN: $Id: AnalyzerTests.php 9 2008-12-01 01:17:49Z michael.bevz $
 * @link       http://mantis.dev.tourstart.dk/wiki/TS_Auth_Adapter
 *
 */

class AnalyzerTests extends PHPUnit_Framework_TestCase
{
    /**
     * Analyzer class
     *
     * @var TV_Text_Analyzer
     */
    private $_analizer;
    
    /**
     * Text for analysis
     *
     * @var string
     */
    private $_text;
    
    protected function setUp()
    {
        $this->_text = 'Duct Tape Marketing Twitter for Business Live Webinar I get a lot of requests for more information about how businesses are using the hot social media Twitter for business. So, I’m teaming up with social media rockstar, Chris Brogan, to present a live webinar called aptly - Twitter for Business.';
        $this->_analizer = new TV_Text_Analyzer();
    }

    protected function tearDown()
    {
      unset($this->_analizer);
    }
    
    /**
     * @expectedException TV_Text_Exception
     *
     */
    public function testWordCountAcceptsOnlyString()
    {
        $this->_analizer->wordCount(array());
    }
    
    public function testWordCountReturnNumberOfWordsInTheText()
    {
        
        $count = $this->_analizer->wordCount($this->_text);
        $this->assertEquals('51', $count);
    }
    
    /**
     * @expectedException TV_Text_Exception
     *
     */
    public function testSyllablesCountAcceptsOnlyString()
    {
        $this->_analizer->syllablesCount(array());
    }
    
    public function testSyllablesCountReturnsNumberOfSyllabled()
    {
        $count = $this->_analizer->syllablesCount($this->_text);
        $this->assertEquals('88', $count);
    }
    
   /**
     * @expectedException TV_Text_Exception
     *
     */
    public function testGetWordsAcceptsOnlyString()
    {
        $this->_analizer->getWords(array());
    }
    
    public function testGetWordsReturnsArrayOfWordsInAlphabetOrderWithoutDuplicatesFromTheText()
    {
        $expected = array('dummy', 'some', 'text');
        $result   = $this->_analizer->getWords('some dummy text text some some');
        $this->assertEquals($expected, $result);
    }
    
   /**
     * @expectedException TV_Text_Exception
     *
     */
    public function testGetWordsStatAcceptsOnlyString()
    {
        $this->_analizer->getWordsStat(array());
    }
    
    public function testGetWordsStatReturnsArrayWithWordsFrequency()
    {
        $expected = array('hello' => 3, 'some' => 2, 'haba'=>2, 'and' => 1, 'hi' =>1);
        $result   = $this->_analizer->getWordsStat('hello hello hello some some and hi haba haba');
        $this->assertEquals($result, $expected);
    }
    
    /**
     * @expectedException TV_Text_Exception
     * @expectedException TV_Text_Exception
     */
    public function testGetPhrasesAcceptsOnlyStringAndInt()
    {
        $this->_analizer->getPhrases(array(), 10);
        $this->_analizer->getPhrases('string', array());
    }
    
    public function testGetPhrasesReturnArrayOfPhasesFromTheText()
    {
        $text = "this is some strange text";
        $expected = array('this is', 'is some', 'some strange', 'strange text');
        $result = $this->_analizer->getPhrases($text, 2);
        //Zend_Debug::dump($expected);
        //Zend_Debug::dump($result);
        
        $this->assertEquals($expected, $result);
        
        $expected = array('this is some', 'is some strange', 'some strange text');
        $result = $this->_analizer->getPhrases($text, 3);
        
        $this->assertEquals($expected, $result);
        
        $text = "this is some strange text with funny sense";
        
        $expected = array('this is some strange', 'is some strange text',
                          'some strange text with', 'strange text with funny'
                          , 'text with funny sense');
        $result = $this->_analizer->getPhrases($text, 4);
        
        $this->assertEquals($expected, $result);
        
        $expected = array('this is some strange text', 'is some strange text with', 'some strange text with funny'
                        , 'strange text with funny sense');
        $result = $this->_analizer->getPhrases($text, 5);
        
        $this->assertEquals($expected, $result);
        
        $text = 'Some other dummy text to test. Text to test.';
        $expected = array('Some other dummy', 'other dummy text', 'dummy text to',
                          'text to test', 'Text to test');
        $result = $this->_analizer->getPhrases($text, 3);
        //Zend_Debug::dump($expected);
        //Zend_Debug::dump($result);
        $this->assertEquals($expected, $result);
        
        $text = 'Text to test. Text to test.';
        $expected = array('Text to test');
        $result = $this->_analizer->getPhrases($text, 3);
        //Zend_Debug::dump($expected);
        //Zend_Debug::dump($result);
        $this->assertEquals($expected, $result);
        
        
        
    }
    
    public function testAnalyzeSentenceReturnsListOfPhrasesFromSentenceUsingComas()
    {
        $text = "This is some, strange text.";
        $expected = array('This is', 'is some', 'strange text');
        $result = $this->_analizer->analyzeSentence($text, 2);
        //Zend_Debug::dump($expected);
        //Zend_Debug::dump($result);
        $this->assertEquals($expected, $result);
        
        
        $text = "This is some, strange, very very strange";
        $expected = array('This is', 'is some', 'very very', 'very strange');
        
        $result = $this->_analizer->analyzeSentence($text, 2);
        //Zend_Debug::dump($expected);
        //Zend_Debug::dump($result);

        
        $this->assertEquals($expected, $result);

        $text = "This is some, strange text.";
        $expected = array('This is some');
        $result = $this->_analizer->analyzeSentence($text, 3);
        $this->assertEquals($expected, $result);
        
        $text = "this is some strange, text with funny sense";
        
        $expected = array('this is some strange', 'text with funny sense');
        $result = $this->_analizer->analyzeSentence($text, 4);
        $this->assertEquals($expected, $result);
        
        $text = "this is some strange text, text with funny sense, limited to 5 words text";
        $expected = array('this is some strange text', 'limited to 5 words text');
        $result = $this->_analizer->analyzeSentence($text, 5);
        $this->assertEquals($expected, $result);
        
    }
    
    public function testRegExp()
    {
        $this->assertEquals(1, preg_match('/,.*,$/', 'some, strange,'));
        $this->assertEquals(0, preg_match('/,.*,$/', 'some strange,'));
        
        $this->assertEquals(0, preg_match('/,.*,$/', 'some strange,'));
        $this->assertEquals(0, preg_match('/,.*,$/', 'some strange'));
    }
    
        
    /**
     * @expectedException TV_Text_Exception
     * @expectedException TV_Text_Exception
     */
    public function testGetPhrasesStatAcceptsOnlyStringAndInt()
    {
        $this->_analizer->getPhrasesStat(array(), 10);
        $this->_analizer->getPhrasesStat('string', array());
    }
    
    public function testGetPhrasesStatReturnsFrequencyOfThePhrasesWithinTheText()
    {
        
        $text = 'Some other dummy text to test. Text to test.';
        
        $phrase = 'text to test';
        $matches = array();
        $result = preg_match_all("/\b$phrase\b/i", $text, $matches);
        $expected = 2;
        $this->assertEquals($expected, $result);
        
        
        $expected = array('some other dummy' => 1, 'other dummy text' =>1,
                           'dummy text to' => 1, 'text to test' => 2);
        $result = $this->_analizer->getPhrasesStat($text, 3);
        $this->assertEquals($expected, $result);
        
        $text = 'Some other dummy text to test. Text to test.
                 Some other dummy text to test. Text to test.';
        
        $expected = array('some other dummy text' => 2, 'other dummy text to'=>2,
                          'dummy text to test' => 2);
        $result = $this->_analizer->getPhrasesStat($text, 4);
        $this->assertEquals($expected, $result);
        
        
        $expected = array('some other dummy text to' => 2, 'other dummy text to test'=>2);
        $result = $this->_analizer->getPhrasesStat($text, 5);
        $this->assertEquals($expected, $result);
        
        
    }
    
    
    
}